using System;
using System.Security;
using System.Security.Principal;
using Castle.Core;
using WindsorContainer.Domain;

namespace WindsorContainer.Service
{
   // [Interceptor(typeof(ISecurity))]
    public class Security : ISecurity
    {
        private ICustomerService service;
        private int numberOfTries = 0;

        public Security(ICustomerService service)
        {
            this.service = service;
        }

        public bool IsAllowed(string userName)
        {
            numberOfTries++;
            Console.Out.WriteLine("numberOfTries = {0}", numberOfTries);
            if(numberOfTries <= 5)
            {
                return userName == WindowsIdentity.GetCurrent().Name;
            }

            return false;
        }

        public Customer FindCustomerById(string userName, int customerId)
        {
            if (IsAllowed(userName))
                return service.FindCustomerById(customerId);

            throw new SecurityException("You are not allowed to find customers");
        }
    }
}
